
package com.feib.soeasy.action;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.feib.soeasy.model.FormatDefinition;
import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.LabelValue;
import com.feib.soeasy.model.Payment;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.FormatDefinitionService;
import com.feib.soeasy.service.GroupService;
import com.feib.soeasy.service.PaymentService;
import com.feib.soeasy.service.Soez5002Service;
import com.feib.soeasy.util.UserUtil;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.DateTypeConverter;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationErrors;


/**
 * @title (#)Soez5002ActionBean.java<br>
 * @description 查詢繳費明細<br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2011/2/14 create by Jimmy Liu<br>
 */
public class Soez5002ActionBean extends BaseActionBean {

	private static final String DEFAULT_JSP = "/WEB-INF/views/soez5002/soez5002.jsp";

	/**
	 * 繳費記錄查詢結果畫面
	 */
	private static final String AJAX_QUERY_JSP = "/WEB-INF/views/soez5002/soez5002List.jsp";

	/**
	 * 業者代號查詢畫面
	 */
	private static final String AJAX_QUERY_GROUP_JSP = "/WEB-INF/views/soez5002/groupPopUpList.jsp";

	/**
	 * 繳費單檔案內容
	 */
	private static final String AJAX_QUERY_PAYMENT_JSP = "/WEB-INF/views/soez5002/paymentList.jsp";

	/**
	 * 繳費單檔案內容
	 */
	private static final String AJAX_WRITEOFF_PAYMENT_JSP = "/WEB-INF/views/soez5002/writeOffpayment.jsp";

	/**
	 * [業者代號]改變後觸發重製[收款類別]選項結果
	 */
	private static final String AJAX_FORMT_DEF_OPTIONS_JSP = "/WEB-INF/views/soez5002/formatDefinitionOptions.jsp";

	/**
	 * 核銷處理結果畫面
	 */
	private static final String AJAX_PAYMENT_WRITEOFF_JSP = "/WEB-INF/views/soez5002/paymentWriteoffResult.jsp";

	private Payment qryPayment;

	/**
	 * 查詢條件-資料期間 起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFrom1;

	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFrom2;

	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFrom3;
	/**
	 * 查詢條件-資料期間 迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateTo1;
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateTo2;
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateTo3;

	/**
	 * 查詢條件--資料期間類型
	 * 1: 繳費日期
	 * 2: 銷帳日期
	 * 3: 繳費期限
	 */
	private String qryDateType1;
	private String qryDateType2;
	private String qryDateType3;

	/**
	 * 查詢條件-繳費截止日期起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryPayDueFrom;

	/**
	 * 查詢條件-繳費截止日期迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryPayDueTo;

	/**
	 * 查詢條件-銷帳日期起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryWriteOffDateFrom;

	/**
	 * 查詢條件-銷帳日期迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryWriteOffDateTo;

	/**
	 * 查詢條件-繳費交易日期起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryTxDateFrom;

	/**
	 * 查詢條件-繳費交易日期迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryTxDateTo;

	/**
	 * 查詢條件-收款類別
	 */
	private FormatDefinition qryFormatDefinition;


	/**
	 * 查詢結果
	 */
	private PaginatedList<Payment> queryResult;

	/**
	 * 帳單業者查詢條件
	 */
	private Group qryGroup;

	/**
	 * 帳單業者查詢結果
	 */
	private PaginatedList<Group> queryGroupResult;

	/**
	 * 查詢單筆Payment
	 */
	private Payment resultPayment;

	/**
	 * 銷帳處理結果
	 */
	private boolean writeoffed;

	private List<Payment> checkPayment;

	/**
	 * 查詢結果新增筆數與金額統計，若只選擇待繳款則僅統計待繳款筆數/金額。
	 * a.全  部筆數 / 金額(全部狀態皆顯示)
	 * b.已繳款  筆數 / 金額(僅顯示一項)
	 * c.待繳款  筆數 / 金額(僅顯示一項)
	 * d.逾  期筆數 / 金額(僅顯示一項)
	 * e.註銷中   筆數 / 金額(僅顯示一項)
	 * f.已註銷   筆數 / 金額(僅顯示一項)
	 */
	private List<Map<String,Object>> querySumByPayment;

	/**
	 * 逾期查詢次數
	 */
	private  String  delayCount;


	/**
	 * 收款類別選單
	 */
	private List<LabelValue> formatDefinitionoOptions = null;

	@SpringBean("paymentService")
	private PaymentService paymentService;

	@SpringBean("groupService")
	private GroupService groupService;

	@SpringBean("formatDefinitionService")
	private FormatDefinitionService formatDefinitionService;

	@SpringBean("soez5002Service")
	private Soez5002Service soez5002Service;

	@DontBind
	@DefaultHandler
	public Resolution doDefault()
	{
		logger.debug("Enter doDefault mehtod.");
		try {

			if (!UserUtil.isBankGroup())
			{
				if (null == this.qryPayment)
					this.qryPayment = new Payment();
				this.qryPayment.setGroup(UserUtil.getUserGroup());
			}

			Calendar cal = Calendar.getInstance();
			qryDateTo1 = cal.getTime();
			qryDateTo2 = cal.getTime();
			qryDateTo3 = cal.getTime();
			cal.add(Calendar.MONTH, -1);
			qryDateFrom1 = cal.getTime();
			qryDateFrom2 = cal.getTime();
			qryDateFrom3 = cal.getTime();
		}
		catch (Exception e) {
		}
		return new ForwardResolution(DEFAULT_JSP); 
	}

	public Resolution doAjaxQuery()
	{
		logger.debug("Enter doAjaxQuery mehtod.");
		try {

			//USER未輸入查詢條件，所以查詢物件會是NULL(查詢物件不可為NULL)
			if (null == this.qryPayment)
				this.qryPayment = new Payment();


			//如果不是銀行端，則查詢條件要加入帳單業者限制
			if (!UserUtil.isBankGroup())
			{    
				Group group = new Group();
				group.setGroupPk(UserUtil.getUserGroup().getGroupPk());
				qryPayment.setGroup(group);
			}
			//呼叫服務查詢
			//queryResult = paymentService.queryByPayment(qryPayment, qryPayDueFrom, qryPayDueTo, queryResult, "Q");
			if (this.qryDateType1!=null)
			{
				// 繳費日期
				this.qryTxDateFrom = this.qryDateFrom1;
				this.qryTxDateTo = this.qryDateTo1;
			}else{
				this.qryTxDateFrom = null;
				this.qryTxDateTo = null;
			}
			if (this.qryDateType2!=null)
			{
				// 銷帳日期
				this.qryWriteOffDateFrom = this.qryDateFrom2;
				this.qryWriteOffDateTo = this.qryDateTo2;
			}else{

				this.qryWriteOffDateFrom = null;
				this.qryWriteOffDateTo = null;
			}

			if (this.qryDateType3!=null)
			{
				// 繳費期限
				this.qryPayDueFrom = this.qryDateFrom3;
				this.qryPayDueTo = this.qryDateTo3;
			}else{
				this.qryPayDueFrom  = null;
				this.qryPayDueTo  = null;

			}
			queryResult = paymentService.queryByPaymentSql(qryPayment, qryFormatDefinition
					, qryPayDueFrom, qryPayDueTo
					, qryWriteOffDateFrom, qryWriteOffDateTo
					, qryTxDateFrom, qryTxDateTo
					, queryResult);
			/* 畫面總計 */
			querySumByPayment = paymentService.querySumByPayment(qryPayment, qryFormatDefinition
					, qryPayDueFrom, qryPayDueTo
					, qryWriteOffDateFrom, qryWriteOffDateTo
					, qryTxDateFrom, qryTxDateTo
			);

		}
		catch (Exception e) {
			logger.error("查詢繳費明細查詢(Soez5002ActionBean.doAjaxQuery):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new SimpleError("查詢錯誤") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		return new ForwardResolution(AJAX_QUERY_JSP);
	}

	/**
	 * TODO
	 * 輸入逾期次數後，帶出符合次數之帳單(須包含逾期+待繳款之帳單)
	 * @author Jeff Gu
	 * @return
	 */
	public Resolution doAjaxDelayQuery()
	{
		logger.debug("Enter doAjaxDelayQuery mehtod.");
		try {

			//USER未輸入查詢條件，所以查詢物件會是NULL(查詢物件不可為NULL)
			if (null == this.qryPayment)
				this.qryPayment = new Payment();
			boolean isSchool = false;
			
			if(null!=qryPayment.getGroup() && null!=qryPayment.getGroup().getGroupNo()){
				Group group	=groupService.findByExactGroupNo(qryPayment.getGroup().getGroupNo());
				isSchool =Group.GROUP_TYPE_SCHOOL.equals(group.getGroupType());
				qryPayment.setGroup(group);
			}
			//如果不是銀行端，則查詢條件要加入帳單業者限制
			if (!UserUtil.isBankGroup())
			{    
				Group group = new Group();
				group.setGroupPk(UserUtil.getUserGroup().getGroupPk());
				isSchool = UserUtil.isSchoolGroup();
				qryPayment.setGroup(group);
			}
			
			//呼叫服務查詢
			if (this.qryDateType1!=null)
			{
				// 繳費日期
				this.qryTxDateFrom = this.qryDateFrom1;
				this.qryTxDateTo = this.qryDateTo1;
			}else{
				this.qryTxDateFrom = null;
				this.qryTxDateTo = null;
			}
			if (this.qryDateType2!=null)
			{
				// 銷帳日期
				this.qryWriteOffDateFrom = this.qryDateFrom2;
				this.qryWriteOffDateTo = this.qryDateTo2;
			}else{

				this.qryWriteOffDateFrom = null;
				this.qryWriteOffDateTo = null;
			}

			if (this.qryDateType3!=null)
			{
				// 繳費期限
				this.qryPayDueFrom = this.qryDateFrom3;
				this.qryPayDueTo = this.qryDateTo3;
			}else{
				this.qryPayDueFrom  = null;
				this.qryPayDueTo  = null;

			}
			if(null==delayCount){
				delayCount="0";
			}
			
			queryResult = paymentService.queryDelayPaymentBySql(qryPayment, qryFormatDefinition
					, qryPayDueFrom, qryPayDueTo
					, qryWriteOffDateFrom, qryWriteOffDateTo
					, qryTxDateFrom, qryTxDateTo
					, queryResult,isSchool,Integer.parseInt(delayCount));
			/* 畫面總計 */
			querySumByPayment = paymentService.querySumDelayPayment(qryPayment, qryFormatDefinition
					, qryPayDueFrom, qryPayDueTo
					, qryWriteOffDateFrom, qryWriteOffDateTo
					, qryTxDateFrom, qryTxDateTo,isSchool,Integer.parseInt(delayCount));


		}
		catch (Exception e) {
			logger.error("查詢繳費明細查詢(Soez5002ActionBean.doAjaxDelayQuery):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new SimpleError("查詢錯誤") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		return new ForwardResolution(AJAX_QUERY_JSP);
	}


	/**
	 * 畫面上列印按鈕(列印全部)
	 * 須設定起始頁數為1，且每頁筆數
	 * @return
	 */
	public Resolution doAjaxQueryPrint()
	{
		logger.debug("Enter doAjaxQueryPrint mehtod.");
		if (null == queryResult)
			queryResult = new PaginatedList<Payment>();

		queryResult.setPageNumber(1);
		queryResult.setPageSize(-1);

		return this.doAjaxQuery();
	}
	
	
	public Resolution doAjaxDownloadDelayExcelFile()
	{        
		logger.debug("Enter doAjaxDownloadDelayExcelFile mehtod.");
		try {
		
			//USER未輸入查詢條件，所以查詢物件會是NULL(查詢物件不可為NULL)
			if (null == this.qryPayment)
				this.qryPayment = new Payment();
			boolean isSchool = false;
			
			if(null!=qryPayment.getGroup() && null!=qryPayment.getGroup().getGroupNo()){
				Group group	=groupService.findByExactGroupNo(qryPayment.getGroup().getGroupNo());
				isSchool =Group.GROUP_TYPE_SCHOOL.equals(group.getGroupType());
				qryPayment.setGroup(group);
			}
			//如果不是銀行端，則查詢條件要加入帳單業者限制
			if (!UserUtil.isBankGroup())
			{    
				Group group = new Group();
				group.setGroupPk(UserUtil.getUserGroup().getGroupPk());
				group.setGroupNo(UserUtil.getUserGroup().getGroupNo());
				isSchool = UserUtil.isSchoolGroup();
				qryPayment.setGroup(group);
			}
			
			if(null==delayCount){
				delayCount="0";
			}
			
			
			//if ( "Y" == this.qryDateType1.toString())
			if ("Y".equalsIgnoreCase(this.qryDateType1))
			{
				// 繳費日期
				this.qryTxDateFrom = this.qryDateFrom1;
				this.qryTxDateTo = this.qryDateTo1;
			}else{
				this.qryTxDateFrom = null;
				this.qryTxDateTo = null;
			}

			//if ( "Y" == this.qryDateType2.toString())
			if ("Y".equalsIgnoreCase(this.qryDateType2))
			{
				// 銷帳日期
				this.qryWriteOffDateFrom = this.qryDateFrom2;
				this.qryWriteOffDateTo = this.qryDateTo2;
			}else{

				this.qryWriteOffDateFrom = null;
				this.qryWriteOffDateTo = null;
			}

			//if ( "Y" == this.qryDateType3.toString())
			if ("Y".equalsIgnoreCase(this.qryDateType3))
			{
				// 繳費期限
				this.qryPayDueFrom = this.qryDateFrom3;
				this.qryPayDueTo = this.qryDateTo3;
			}else{
				this.qryPayDueFrom  = null;
				this.qryPayDueTo  = null;

			}

			//呼叫服務查詢
			String downloadFilePath = soez5002Service.doExportDelayXls(qryPayment, qryFormatDefinition, qryPayDueFrom, qryPayDueTo, qryWriteOffDateFrom, qryWriteOffDateTo, qryTxDateFrom, qryTxDateTo,isSchool,Integer.parseInt(delayCount));

			if (null == downloadFilePath)
			{
				// 查無資料
				logger.debug("查無資料");
			}

			File downloadFile = new File(downloadFilePath);
			byte[] bytes = new byte[(int)downloadFile.length()];

			BufferedInputStream bufferedInputStream = 
				new BufferedInputStream(new FileInputStream(downloadFile)); 

			bufferedInputStream.read(bytes);

			logger.debug("下載檔案大小:{}",bytes.length );

			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
			inputStream.close();
			bufferedInputStream.close();

			String isoFileName = new String("繳款狀況明細表.xls".getBytes("MS950"), "ISO8859-1");

			StreamingResolution streamingResolution = new StreamingResolution( 
					"application/vnd.ms-excel",inputStream).setFilename(isoFileName);

			return streamingResolution;
		}
		catch (Exception e) {
			logger.error("查詢繳費明細查詢(Soez5002ActionBean.doAjaxDownloadDelayExcelFile):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new SimpleError("查詢錯誤") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		//return new ForwardResolution(AJAX_QUERY_JSP);
	}


	public Resolution doAjaxDownloadExcelFile()
	{        
		logger.debug("Enter doAjaxDownloadExcelFile mehtod.");
		try {

			//USER未輸入查詢條件，所以查詢物件會是NULL(查詢物件不可為NULL)
			if (null == this.qryPayment)
				this.qryPayment = new Payment();


			//如果不是銀行端，則查詢條件要加入帳單業者限制
			if (!UserUtil.isBankGroup())
			{
				Group group = new Group();
				group.setGroupNo(UserUtil.getUserGroup().getGroupNo());
				group.setGroupPk(UserUtil.getUserGroup().getGroupPk());
				qryPayment.setGroup(group);
			}

			//if ( "Y" == this.qryDateType1.toString())
			if ("Y".equalsIgnoreCase(this.qryDateType1))
			{
				// 繳費日期
				this.qryTxDateFrom = this.qryDateFrom1;
				this.qryTxDateTo = this.qryDateTo1;
			}else{
				this.qryTxDateFrom = null;
				this.qryTxDateTo = null;
			}

			//if ( "Y" == this.qryDateType2.toString())
			if ("Y".equalsIgnoreCase(this.qryDateType2))
			{
				// 銷帳日期
				this.qryWriteOffDateFrom = this.qryDateFrom2;
				this.qryWriteOffDateTo = this.qryDateTo2;
			}else{

				this.qryWriteOffDateFrom = null;
				this.qryWriteOffDateTo = null;
			}

			//if ( "Y" == this.qryDateType3.toString())
			if ("Y".equalsIgnoreCase(this.qryDateType3))
			{
				// 繳費期限
				this.qryPayDueFrom = this.qryDateFrom3;
				this.qryPayDueTo = this.qryDateTo3;
			}else{
				this.qryPayDueFrom  = null;
				this.qryPayDueTo  = null;

			}

			//呼叫服務查詢
			String downloadFilePath = soez5002Service.doExportXls(qryPayment, qryFormatDefinition, qryPayDueFrom, qryPayDueTo, qryWriteOffDateFrom, qryWriteOffDateTo, qryTxDateFrom, qryTxDateTo);

			if (null == downloadFilePath)
			{
				// 查無資料
				logger.debug("查無資料");
			}

			File downloadFile = new File(downloadFilePath);
			byte[] bytes = new byte[(int)downloadFile.length()];

			BufferedInputStream bufferedInputStream = 
				new BufferedInputStream(new FileInputStream(downloadFile)); 

			bufferedInputStream.read(bytes);

			logger.debug("下載檔案大小:{}",bytes.length );

			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
			inputStream.close();
			bufferedInputStream.close();

			String isoFileName = new String("繳款狀況明細表.xls".getBytes("MS950"), "ISO8859-1");

			StreamingResolution streamingResolution = new StreamingResolution( 
					"application/vnd.ms-excel",inputStream).setFilename(isoFileName);

			return streamingResolution;
		}
		catch (Exception e) {
			logger.error("查詢繳費明細查詢(Soez5002ActionBean.doAjaxQuery):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new SimpleError("查詢錯誤") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		//return new ForwardResolution(AJAX_QUERY_JSP);
	}

	/**
	 * 帳單業者查詢
	 * @return
	 */
	public Resolution doAjaxQueryGroups()
	{
		logger.debug("Enter doAjaxQueryGroups mehtod.");
		try {
			//如果不是銀行端，則查詢條件要加入帳單業者限制
			if (!UserUtil.isBankGroup())
			{
				throw new Exception("No Authority");
			}


			//USER未輸入查詢條件，所以查詢物件會是NULL(查詢物件不可為NULL)
			if (null == qryGroup)
				qryGroup = new Group();

			//呼叫服務查詢
			queryGroupResult = groupService.queryByGroupExcludeBank(qryGroup, queryGroupResult);
		}
		catch (Exception e) {
			logger.error("查詢繳費明細-帳單業者查詢錯誤(Soez5002ActionBean.doAjaxQueryGroups):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new SimpleError("查詢錯誤") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		return new ForwardResolution(AJAX_QUERY_GROUP_JSP);
	}

	public Resolution doAjaxGroupNoChange()
	{
		logger.debug("Enter doAjaxGroupNoChange mehtod.");
		return new ForwardResolution(AJAX_FORMT_DEF_OPTIONS_JSP);
	}

	/**
	 * 查詢轉入的帳單資料
	 * @return
	 */
	public Resolution doAjaxQueryPayment()
	{
		logger.debug("Enter doAjaxQueryPayment mehtod.");
		try {

			//呼叫服務查詢
			resultPayment = paymentService.getPaymentByPk(qryPayment);
		}
		catch (Exception e) {
			logger.error("查詢繳費明細-帳單內容查詢錯誤(Soez5002ActionBean.doAjaxQueryPayment):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new SimpleError("查詢錯誤") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		return new ForwardResolution(AJAX_QUERY_PAYMENT_JSP);
	}

	public Resolution toAjaxWriteoffPayment()
	{
		logger.debug("Enter toAjaxWriteoffPayment mehtod.");
		try {

			//呼叫服務查詢
			resultPayment = paymentService.getPaymentByPk(qryPayment);
		}
		catch (Exception e) {
			logger.error("查詢繳費明細-帳單內容查詢錯誤(Soez5002ActionBean.toAjaxWriteoffPayment):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new SimpleError("查詢錯誤") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		return new ForwardResolution(AJAX_WRITEOFF_PAYMENT_JSP);
	}

	public Resolution doWriteOffPaymentsAction()
	{
		logger.debug("Enter doWriteOffPaymentsAction mehtod.");
		try {
			writeoffed = false;
			paymentService.doWriteOffPayments(qryPayment);
			writeoffed = true;
		}
		catch (Exception e) {
			logger.error("查詢繳費明細-帳單內容查詢錯誤(Soez5002ActionBean.doWriteOffPaymentsAction):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new SimpleError("查詢錯誤") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		return new ForwardResolution(AJAX_PAYMENT_WRITEOFF_JSP);
	}

	public Resolution doNoticeEmail()
	{
		logger.debug("Enter doNoticeEmail mehtod.");
		StringBuilder message = new StringBuilder();
		try {

			if (null != this.checkPayment && 0 != this.checkPayment.size())
			{
				paymentService.doNoticeUnPayByEmail(this.checkPayment);
				message.append("已發送繳款Mail通知");
			}
			else
			{
				message.append("請先勾選未繳款人資料");
			}

		}
		catch (Exception e) {
			logger.error("查詢繳費明細-發送Email通知錯誤(Soez5002ActionBean.doNoticeEmail):", e);

			// 轉換錯誤訊息
			message.append("發送繳款Email通知錯誤");
		}

		StreamingResolution resolution = new StreamingResolution("text/html", new StringReader(message.toString()));
		resolution.setCharacterEncoding("UTF-8");
		return resolution;

	}



	/**
	 * @return the qryPayment
	 */
	public Payment getQryPayment() {
		return qryPayment;
	}


	/**
	 * @param qryPayment the qryPayment to set
	 */
	public void setQryPayment(Payment qryPayment) {
		this.qryPayment = qryPayment;
	}


	/**
	 * @return the qryPayDueFrom
	 */
	public Date getQryPayDueFrom() {
		return qryPayDueFrom;
	}


	/**
	 * @param qryPayDueFrom the qryPayDueFrom to set
	 */
	public void setQryPayDueFrom(Date qryPayDueFrom) {
		this.qryPayDueFrom = qryPayDueFrom;
	}


	/**
	 * @return the qryGroup
	 */
	public Group getQryGroup() {
		return qryGroup;
	}


	/**
	 * @param qryGroup the qryGroup to set
	 */
	public void setQryGroup(Group qryGroup) {
		this.qryGroup = qryGroup;
	}


	/**
	 * @return the queryGroupResult
	 */
	public PaginatedList<Group> getQueryGroupResult() {
		return queryGroupResult;
	}


	/**
	 * @param queryGroupResult the queryGroupResult to set
	 */
	public void setQueryGroupResult(PaginatedList<Group> queryGroupResult) {
		this.queryGroupResult = queryGroupResult;
	}


	/**
	 * @return the queryResult
	 */
	public PaginatedList<Payment> getQueryResult() {
		return queryResult;
	}


	/**
	 * @param queryResult the queryResult to set
	 */
	public void setQueryResult(PaginatedList<Payment> queryResult) {
		this.queryResult = queryResult;
	}


	/**
	 * @return the resultPayment
	 */
	public Payment getResultPayment() {
		return resultPayment;
	}


	/**
	 * @param resultPayment the resultPayment to set
	 */
	public void setResultPayment(Payment resultPayment) {
		this.resultPayment = resultPayment;
	}

	/**
	 * @return the formatDefinitionoOptions
	 */
	public List<LabelValue> getFormatDefinitionoOptions() {
		if (null == this.formatDefinitionoOptions)
		{
			//如果不是銀行端，則查詢條件要加入帳單業者限制
			if (!UserUtil.isBankGroup())
			{
				// 查詢條件
				FormatDefinition formatDefinition = new FormatDefinition();
				Group group = new Group();
				group.setGroupPk(UserUtil.getUserGroup().getGroupPk());
				formatDefinition.setGroup(group);
				formatDefinition.setFormatType(new Integer(1));
				formatDefinition.setDeleted(Boolean.FALSE);
				formatDefinitionoOptions = formatDefinitionService.findLabelValueByFormatDefinition(formatDefinition, true);
			}
			else
			{
				// 銀行端，看是否有群組代碼，若沒有則回傳空陣列
				if (null != this.qryGroup)
				{
					// 查詢條件
					FormatDefinition formatDefinition = new FormatDefinition();
					formatDefinition.setGroup(this.qryGroup);
					formatDefinition.setDeleted(Boolean.FALSE);
					formatDefinition.setFormatType(new Integer(1));
					formatDefinitionoOptions = formatDefinitionService.findLabelValueByFormatDefinition(formatDefinition, true);
				}
				else
				{
					this.formatDefinitionoOptions = new ArrayList<LabelValue>();                    
				}

			}

		}
		return formatDefinitionoOptions;
	}


	/**
	 * @param formatDefinitionoOptions the formatDefinitionoOptions to set
	 */
	public void setFormatDefinitionoOptions(List<LabelValue> formatDefinitionoOptions) {
		this.formatDefinitionoOptions = formatDefinitionoOptions;
	}


	/**
	 * @return the qryFormatDefinition
	 */
	public FormatDefinition getQryFormatDefinition() {
		return qryFormatDefinition;
	}


	/**
	 * @param qryFormatDefinition the qryFormatDefinition to set
	 */
	public void setQryFormatDefinition(FormatDefinition qryFormatDefinition) {
		this.qryFormatDefinition = qryFormatDefinition;
	}


	/**
	 * @return the writeoffed
	 */
	public boolean isWriteoffed() {
		return writeoffed;
	}


	/**
	 * @param writeoffed the writeoffed to set
	 */
	public void setWriteoffed(boolean writeoffed) {
		this.writeoffed = writeoffed;
	}


	/**
	 * @return the checkPayment
	 */
	public List<Payment> getCheckPayment() {
		return checkPayment;
	}


	/**
	 * @param checkPayment the checkPayment to set
	 */
	public void setCheckPayment(List<Payment> checkPayment) {
		this.checkPayment = checkPayment;
	}

	public Date getQryDateFrom1() {
		return qryDateFrom1;
	}

	public void setQryDateFrom1(Date qryDateFrom1) {
		this.qryDateFrom1 = qryDateFrom1;
	}

	public Date getQryDateFrom2() {
		return qryDateFrom2;
	}

	public void setQryDateFrom2(Date qryDateFrom2) {
		this.qryDateFrom2 = qryDateFrom2;
	}

	public Date getQryDateFrom3() {
		return qryDateFrom3;
	}

	public void setQryDateFrom3(Date qryDateFrom3) {
		this.qryDateFrom3 = qryDateFrom3;
	}

	public Date getQryDateTo1() {
		return qryDateTo1;
	}

	public void setQryDateTo1(Date qryDateTo1) {
		this.qryDateTo1 = qryDateTo1;
	}

	public Date getQryDateTo2() {
		return qryDateTo2;
	}

	public void setQryDateTo2(Date qryDateTo2) {
		this.qryDateTo2 = qryDateTo2;
	}

	public Date getQryDateTo3() {
		return qryDateTo3;
	}

	public void setQryDateTo3(Date qryDateTo3) {
		this.qryDateTo3 = qryDateTo3;
	}

	public String getQryDateType1() {
		return qryDateType1;
	}

	public void setQryDateType1(String qryDateType1) {
		this.qryDateType1 = qryDateType1;
	}

	public String getQryDateType2() {
		return qryDateType2;
	}

	public void setQryDateType2(String qryDateType2) {
		this.qryDateType2 = qryDateType2;
	}

	public String getQryDateType3() {
		return qryDateType3;
	}

	public void setQryDateType3(String qryDateType3) {
		this.qryDateType3 = qryDateType3;
	}

	public List<Map<String, Object>> getQuerySumByPayment() {
		return querySumByPayment;
	}

	public void setQuerySumByPayment(List<Map<String, Object>> querySumByPayment) {
		this.querySumByPayment = querySumByPayment;
	}

	public String getDelayCount() {
		return delayCount;
	}

	public void setDelayCount(String delayCount) {
		this.delayCount = delayCount;
	}



}
